import { createSlice } from "@reduxjs/toolkit";

//创建切片对象
let zanSlice = createSlice({
    name:"zan",
    initialState:{
        value:10
    },
    reducers:{
        zanIncre:function(state,action){
            state.value += action.payload;
        },
        zanDecre(state,{payload}){
            state.value -= payload;
        }
    }
})

//提取 action 方法
export let {zanDecre,zanIncre} = zanSlice.actions;

//创建 zan  异步加 方法
export let asyncZanIncre = (payload)=>{
    return (dispatch)=>{
        setTimeout(()=>{
            dispatch(zanIncre(payload));
        },1000)
    }
}   

//创建 zan  异步减 方法
export let asyncZanDecre = (payload)=>{
    return (dispatch)=>{
        setTimeout(() => {
            dispatch(zanDecre(payload));
        }, 1000);
        
    }
}

//暴露zan  reducer
export default zanSlice.reducer;